package cd.cdyb.sms.listener;

import cd.cdyb.sms.dao.InventoryFlowDao;
import cd.cdyb.sms.domain.InventoryFlow;
import cd.cdyb.sms.utils.JacksonUtilPlus;
import cd.cdyb.sms.utils.RedissonUtil;
import cn.hutool.core.util.IdUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @Author: lilin
 * @CreateTime: 2023-07-28  10:48
 * @Description: 队列监听
 */
@Slf4j
@Component
public class InventoryRabbitListener {

    @Resource
    private InventoryFlowDao inventoryFlowDao;

    @Resource
    private RedissonUtil redissonUtil;

    @RabbitListener(queues = "${mq.queue}")
    public void listener(String obj, Channel channel) {
        insertInventoryFlow(obj);
    }

    @RabbitListener(queues = "${mq.queue}")
    public void listener2(String obj, Channel channel) {
        insertInventoryFlow(obj);
    }

    @RabbitListener(queues = "${mq.queue}")
    public void listener3(String obj, Channel channel) {
        insertInventoryFlow(obj);
    }

    @RabbitListener(queues = "${mq.queue}")
    public void listener4(String obj, Channel channel) {
        insertInventoryFlow(obj);
    }

    @RabbitListener(queues = "${mq.queue}")
    public void listener5(String obj, Channel channel) {
        insertInventoryFlow(obj);
    }

    private void insertInventoryFlow(String obj) {

        InventoryFlow inventoryFlow = JacksonUtilPlus.readValue(obj, InventoryFlow.class);
        String randomUUID = IdUtil.randomUUID();
        inventoryFlow.setNumber(randomUUID);
        inventoryFlow.setWhetherRecording(false);
        inventoryFlowDao.insert(inventoryFlow);

    }
}
